package com.tfswue.cxy.service;

import com.tfswue.cxy.entity.BorrowRecord;
import com.tfswue.cxy.feign.BookFeignClient;
import com.tfswue.cxy.mapper.BorrowRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class BorrowService {
    @Autowired
    private BorrowRecordMapper borrowRecordMapper;
    @Autowired
    private BookFeignClient bookFeignClient;

    // 借书
    public boolean borrowBook(Long userId, Long bookId) {
        Object book = bookFeignClient.getBookById(bookId);
        if (book == null) {
            return false;
        }
        BorrowRecord record = new BorrowRecord();
        record.setUserId(userId);
        record.setBookId(bookId);
        record.setBorrowDate(new Date());
        borrowRecordMapper.insert(record);
        return true;
    }

    // 查询所有借阅记录
    public List<BorrowRecord> getAllRecords() {
        return borrowRecordMapper.selectList(null);
    }

    // 查询单条借阅记录
    public BorrowRecord getRecordById(Long id) {
        return borrowRecordMapper.selectById(id);
    }

    // 删除借阅记录
    public void deleteRecord(Long id) {
        borrowRecordMapper.deleteById(id);
    }

    // 修改借阅记录
    public void updateRecord(BorrowRecord record) {
        borrowRecordMapper.updateById(record);
    }
}